package com.xilihui.xlh.business.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.common.activitylistener.BaseActivityListener;
import com.weigan.loopview.LoopView;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.dialogs.AddressPickerDialog;
import com.xilihui.xlh.business.dialogs.PickerViewDialog;
import com.xilihui.xlh.business.entities.AreaEntity;
import com.xilihui.xlh.business.entities.WithdrawEntity;
import com.xilihui.xlh.business.requests.MineRequest;
import com.xilihui.xlh.business.requests.SignedRequest;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.app.ToolBaseCompatActivity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.ToastUtil;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/2.
 */

public class WithdrawActivity extends ToolBaseCompatActivity implements View.OnClickListener {
    
    @BindView(R.id.tv_jiner)
    TextView tv_jiner;
    @BindView(R.id.ll_way)
    LinearLayout ll_way;
    @BindView(R.id.et_name)
    EditText et_name;
    @BindView(R.id.ll_kaihuhang)
    LinearLayout ll_kaihuhang;
    @BindView(R.id.ll_address)
    LinearLayout ll_address;
    @BindView(R.id.ll_kahao)
    LinearLayout ll_kahao;
    @BindView(R.id.ll_zhihang)
    LinearLayout ll_zhihang;
    @BindView(R.id.et_zhihang)
    EditText et_zhihang;
    @BindView(R.id.et_kahao)
    EditText et_kahao;
    @BindView(R.id.et_applyJiner)
    EditText et_applyJiner;
    @BindView(R.id.tv_way)
    TextView tv_way;
    @BindView(R.id.tv_kaihuhang)
    TextView tv_kaihuhang;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.tv_shouruJiner)
    TextView tv_shouruJiner;
    @BindView(R.id.tv_hint)
    TextView tv_hint;
    @BindView(R.id.tv_submit)
    TextView tv_submit;
    @BindView(R.id.tv_withdraw)
    TextView tv_withdraw_hint;
    
    @BindView(R.id.tv_hint_kahao)
    TextView tv_hint_kahao;
    @BindView(R.id.view_line)
    View view_line;
    @BindView(R.id.view_line2)
    View view_line2;
    @BindView(R.id.view_line3)
    View view_line3;
    
    ArrayList<WithdrawEntity.ApplicationMethodBean> ways = new ArrayList<>();
    ArrayList<String> zhihangs = new ArrayList<>();
    
    String way = "";
    String name = "";
    String huhangh = "";
    String address = "";
    String zhihang = "";
    String kahao = "";
    String jiner = "";
    
    String mProvince = "";
    String mCity = "";
    
    String type = "";
    
    float rate = 0;
    
    boolean isYinHang = false;
    
    WithdrawEntity withdrawEntity = null;
    
    @Override
    public int getLayout() {
        return R.layout.activity_withdraw;
    }
    
    @OnClick(R.id.iv_toolbar_left)
    public void back(){
        finish();
    }
    
    @OnClick(R.id.tv_toolbar_right)
    public void record(){
        startActivity(new Intent(WithdrawActivity.this,WithdrawRecordActivity.class));
    }

    @Override
    public String initTitle() {
        return null;
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        setToolTitle("提现申请");
        setToolRightText("提现记录");
        
        withdrawEntity = (WithdrawEntity) getIntent().getSerializableExtra("data");
        
        ll_way.setOnClickListener(this);
        ll_address.setOnClickListener(this);
        ll_kaihuhang.setOnClickListener(this);
        tv_submit.setOnClickListener(this);
        
        et_applyJiner.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(charSequence.length()>=1){
                    float num = Float.valueOf(charSequence.toString());
                    float s =  num - num*rate/100;
                    tv_shouruJiner.setText(s+"");
                        
                }else {
                    tv_shouruJiner.setText("");
                }
            }
            
            @Override
            public void afterTextChanged(Editable editable) {
                
            }
        });
        
    }

    @Override
    public void initData() {
        if(withdrawEntity!=null){
            tv_jiner.setText("￥"+withdrawEntity.getUser_info().getMoney());
            LogUtil.i("mylog",withdrawEntity.getWithdrawals_rate()+"");
            rate = Float.valueOf(withdrawEntity.getWithdrawals_rate().trim());
            rate = 100-rate;
            tv_hint.setText("实际到账金额为提现金额扣除"+rate+"%的手续费后计算所得");
            ways = (ArrayList<WithdrawEntity.ApplicationMethodBean>) withdrawEntity.getApplication_method();
            zhihangs = (ArrayList<String>) withdrawEntity.getBank();
            tv_withdraw_hint.setText("每次提现最小金额￥"+withdrawEntity.getMin_money());
        }
    }
    

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_way:
                way();        
                break;
            case R.id.ll_address:
                address();
                break;
            case R.id.ll_kaihuhang:
                kaihuhuang();
                break;
            case R.id.tv_submit:
                submit();
                break;
        }
    }
    
    public void handleShow(int show){
        view_line.setVisibility(show);
        view_line2.setVisibility(show);
        view_line3.setVisibility(show);
        ll_zhihang.setVisibility(show);
        ll_kaihuhang.setVisibility(show);
        ll_address.setVisibility(show);
    }
    
    public void way(){
        new PickerViewDialog<WithdrawEntity.ApplicationMethodBean>(WithdrawActivity.this,ways,0) {
            @Override
            protected void setItems(LoopView loopView, List<WithdrawEntity.ApplicationMethodBean> list) {
                ArrayList<String> data = new ArrayList<>();
                for (int i=0;i<list.size();i++){
                    data.add(list.get(i).getName());
                }
                loopView.setItems(data);
            }
            
            @Override
            protected void onConfirm(WithdrawEntity.ApplicationMethodBean withdrawEntity, int position) {
                String s = withdrawEntity.getName();
                type = withdrawEntity.getType();
                tv_way.setText(s);
                way = s;
                if(s.equals("银行转账")){
                    handleShow(View.VISIBLE);
                    tv_hint_kahao.setText("银行卡号");
                    et_kahao.setHint("请输入银行卡号");
                    isYinHang = true;
                }else if(s.equals("支付宝转账")){
                    handleShow(View.GONE);
                    tv_hint_kahao.setText("支付宝账号");
                    et_kahao.setHint("请输入支付宝账号");
                    isYinHang = false;
                }else if(s.equals("微信转账")){
                    handleShow(View.GONE);
                    tv_hint_kahao.setText("微信账号");
                    et_kahao.setHint("请输入微信账号");
                    isYinHang = false;
                }
            }
            
        }.show();
    }
    
    public void address(){

        final AreaEntity areaEntity = (AreaEntity) SPUtil.getObject(SPUtil.TOKEN);
        
        if(areaEntity!=null){
            AddressPickerDialog dialog = new AddressPickerDialog(this);
            dialog.setCallback(new AddressPickerDialog.Callback() {
                @Override
                public void onSelected(String province, String city, String county, String provinceId, String cityId, String countyId) {
                    address = province+" "+city;
                    mProvince = provinceId;
                    mCity = cityId;
                    tv_address.setText(address);
                }
            });
            dialog.setData(areaEntity);
            dialog.setLayer(AddressPickerDialog.Layer.TWO);
            dialog.show();
        }else {
            SignedRequest.getAddress(this)
                    .compose(DoTransform.<AreaEntity>applyScheduler(this,true))
                    .compose(WithdrawActivity.this.<AreaEntity>bindToLifecycle())
                    .subscribe(new HttpSubscriber<AreaEntity>(this) {
                        @Override
                        protected void onSuccess(AreaEntity entity) {
                            AddressPickerDialog dialog = new AddressPickerDialog(WithdrawActivity.this);
                            dialog.setCallback(new AddressPickerDialog.Callback() {
                                @Override
                                public void onSelected(String province, String city, String county, String provinceId, String cityId, String countyId) {
                                    address = province +" "+city;
                                    tv_address.setText(address);
                                    mProvince = provinceId;
                                    mCity = cityId;
                                }
                            });
                            dialog.setData(entity);
                            dialog.setLayer(AddressPickerDialog.Layer.TWO);
                            dialog.show();
                        }

                        @Override
                        protected void onFail(String msg) {
                            ToastUtil.toastShortNegative(msg);
                        }
                    });
        }
        
    }
    
    public void kaihuhuang(){
        new PickerViewDialog<String>(WithdrawActivity.this,zhihangs,0){

            @Override
            protected void setItems(LoopView loopView, List<String> list) {
                loopView.setItems(list);
            }

            @Override
            protected void onConfirm(String s, int position) {
                huhangh = s;
                tv_kaihuhang.setText(s);
            }
        }.show();
    }
    
    public void submit(){
        
        if(TextUtil.isEmpty(way)){
            ToastUtil.toastShortNegative("请选择提现方式");
            return;
        }
        name = et_name.getText().toString();
        if(TextUtil.isEmpty(name)){
            ToastUtil.toastShortNegative("请输入姓名");
            return;
        }
        
        if(isYinHang){
            if(TextUtil.isEmpty(huhangh)){
                ToastUtil.toastShortNegative("请选择开户行");
                return;
            }
            
            if(TextUtil.isEmpty(address)){
                ToastUtil.toastShortNegative("请选择开户行所在地");
                return;
            }
            
            zhihang = et_zhihang.getText().toString();
            if(TextUtil.isEmpty(zhihang)){
                ToastUtil.toastShortNegative("请输入所属支行");
                return;
            }
        }else {
            huhangh = "";
            address = "";
            zhihang = "";
        }
        
        
        kahao = et_kahao.getText().toString();
        if(TextUtil.isEmpty(kahao)){
            if(isYinHang){
                ToastUtil.toastShortNegative("请输入银行卡号");
            }else {
                ToastUtil.toastShortNegative("请输入账号");
            }
            return;
        }
        
        jiner = et_applyJiner.getText().toString().trim();
        if(TextUtil.isEmpty(jiner)){
            ToastUtil.toastShortNegative("请输入提现金额");
            return;
        }
        
        
        MineRequest.applyWithdraw(this,jiner,huhangh,kahao,name,mProvince,mCity,zhihang,type)
                .compose(DoTransform.<BaseEntity>applyScheduler(this,true))
                .compose(WithdrawActivity.this.<BaseEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<BaseEntity>(WithdrawActivity.this) {
                    @Override
                    protected void onSuccess(BaseEntity entity) {
                        ToastUtil.toastShortPositive(entity.getMsg());
                        finish();
                    }
                    
                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
        
        
    }
    
}
